package main

import (
	"emicro_test/emicro_3/grpc_gateway/common"
	"emicro_test/emicro_3/grpc_gateway/router"
	_ "emicro_test/emicro_3/grpc_gateway/utils"
	"flag"
	"fmt"
	httpServer "github.com/asim/go-micro/plugins/server/http/v3"
	"github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/registry"
	"github.com/asim/go-micro/v3/server"
	"github.com/go-micro/plugins/v3/registry/consul"
	"log"
)


func main(){

	var address string
	flag.StringVar(&address,"gate_address", ":55001", "gateway api address")
	flag.Parse()


	//服务注册
	consulRegis := consul.NewRegistry( func(options *registry.Options){
		addr := common.EConfig.String("consul_addr")
		port, _ := common.EConfig.Int("consul_port")
		addrs := fmt.Sprintf("%v:%d", addr, port)
		options.Addrs = []string{
			addrs,
		}
	})

	//创建web服务
	srv := httpServer.NewServer(
		server.Name(common.ServerName),
		server.Address(address),
	)
	//注册路由和初始化路由
	rou := router.Router()
	hd := srv.NewHandler(rou)
	if err := srv.Handle(hd); err != nil {
		log.Fatalf("Http Handle Err:%v", err)
	}

	//启动服务
	common.Service = micro.NewService(
		micro.Server(srv),
		micro.Registry(consulRegis),
	)
	//common.Service.Init()
	if err := common.Service.Run(); err != nil {
		log.Fatalf("Http Service Err:%v", err)
	}

}
